package remote_work.client;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;

import java.io.File;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

import remote_work.dto.InputObject;
import remote_work.dto.OutputObject;
import remote_work.util.RequestService;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class AddAssignUI {

	protected Shell shell;
	private Text text;
	
	private InputObject inputObject;
	
	public void setInputObject(InputObject inputObject) {
		this.inputObject = inputObject;
	}
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			AddAssignUI window = new AddAssignUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(MainUI.shell);
		shell.setSize(450, 300);
		shell.setText("添加作业");
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("作业截止时间：");
		label.setBounds(42, 44, 90, 20);
		
		DateTime dateTime = new DateTime(shell, SWT.BORDER);
		dateTime.setBounds(138, 36, 98, 28);
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fd = new FileDialog(shell);
				fd.open();
				text.setText(fd.getFilterPath()+ "\\" + fd.getFileName());
			}
		});
		button.setBounds(324, 105, 98, 30);
		button.setText("选择作业");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(138, 109, 162, 26);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBounds(42, 112, 90, 20);
		label_1.setText("选中的作业：");
		
		DateTime dateTime_1 = new DateTime(shell, SWT.BORDER | SWT.TIME);
		dateTime_1.setBounds(257, 36, 88, 28);
		
		Button button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String validDate = dateTime.getYear()+"-"+(dateTime.getMonth()+1)+"-"+dateTime.getDay();
				int minutes = dateTime_1.getMinutes();
				String minute=dateTime_1.getMinutes()+"";
				if(minutes<10){
					minute="0"+minutes;
				}
				int seconds = dateTime_1.getSeconds();
				String second=dateTime_1.getSeconds()+"";
				if(seconds<10){
					second="0"+seconds;
				}
				validDate+=" "+dateTime_1.getHours()+":"+minute+":"+second;
				String filePath = text.getText();
				if (filePath != null && !"".equals(filePath)) {
					File file = new File(filePath);
					inputObject.setMethod("uploadFile");
					inputObject.addParams("uploadPerson", MainUI.getUser().get("username"));
					inputObject.addParams("validDate", validDate);
					inputObject.addParams("file", file);
					OutputObject outputObject = RequestService.execute(inputObject);
					MessageBox mb = new MessageBox(shell);
					if ("0".equals(outputObject.getReturnCode())) {
						Map<String, Object> bean = outputObject.getBean();
						MainUI.setParam(bean);
						mb.setMessage("上传作业成功");
					} else {
						mb.setMessage("上传作业失败！！！");
					}
					mb.open();
					shell.dispose();
				}
			}
		});
		button_1.setBounds(99, 192, 98, 30);
		button_1.setText("确认");
		
		Button button_2 = new Button(shell, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		button_2.setBounds(220, 192, 98, 30);
		button_2.setText("取消");
		
		

	}
}
